<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-15
 * Time: 20:56
 */
namespace app\member\controller;

use app\admin\model\UserData;
use app\common\controller\MemberBase;
use app\common\controller\Upload;
use app\common\model\User as cxModel;

class User extends MemberBase {

    public function dangan(){
        $cxmodel = new cxModel();
        $postdb = $cxmodel->where('uid',$this->cxbsuser['uid'])->find();
        $postdb = $postdb->userdb->toArray();
        $postdb['archives'] = unserialize($postdb['archives']);
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $data['archives'] = serialize($data['archives']);
            if(empty($data['uid']) || $data['uid'] != $this->cxbsuser['uid']){
                $this->error('非法访问');
            }
            if(model('UserData')->allowField(true)->isUpdate(true)->save($data,['uid'=>$data['uid']])){
                $user = $cxmodel->where('uid',$data['uid'])->find();
                $users = $user->userdb;
                $users = $users->toArray();
                $users['group_name'] = \model('AuthGroup')->where('id',$users['group_id'])->value('title');
                unset($users['upassword']);
                userCacheSet($user['uid'],'userdb',$users);
                session('userdb',$users);
                $this->success("编辑成功！");
            }else{
                $this->error("编辑失败");
            }
        }
        $this->assign([
            'postdb'=>$postdb,
        ]);
        return view($this->temp.'user_fujia.htm');
    }
    public function edit(){
        $cxmodel = new cxModel();
        $usmodel = new UserData();
        $edit = $usmodel->where('uid',$this->cxbsuser['uid'])->find();
        $edit = $this->usmodelfields($edit);
        if(!empty($edit['ucity'])){
            $edit['ucity'] = explode(',',$edit['ucity']);
        }
        if(request()->isPost()) {
            $data = input('post.');
            $data = datatrim($data);
            if($data['uid'] != $this->cxbsuser['uid']){
                $this->error("非法访问！");
            }
            if(!empty($data['uemail'])){
                $m = model('UserData')->where('uemail',$data['uemail'])->value('uid');
                if(!empty($m) && $m != $data['uid']){
                    $this->error("邮箱已存在！");
                }
            }
            if(!empty($data['newpassword'])){
                $data['upassword'] = pwd($data['newpassword']);
            }
            if(!empty($data['uicon'])){
                if($data['uicon'] != $this->cxbsuser['uicon']){
                    $upmodel = new Upload();
                    $uicon = 'uicon';
                    $data['uicon'] = $upmodel->fileMove($data['uicon'],$uicon);
                    @unlink(ROOT_PATH.$this->webdb['updir'].'/'.$this->cxbsuser['uicon']);
                }
            }
            if(!empty($data['ucity'])){
                $data['ucity'] = implode(',',$data['ucity']);
            }
            if(!empty($data['ubday'])){
                $data['ubday'] = strtotime($data['ubday']);
            }
            $data = $this->addeditbase($data,$this->cxbsuser);
            if(!empty($data['upassword'])){
                $cxmodel->allowField(true)->save($data,$data['uid']);
                if($data['uid'] == 1){
                    unset($data['upassword']);
                }
            }
            if($cxmodel->edit_user($data)){
                $this->cxbsuser = session('userdb');
                $this->success('个人资料修改成功！');
            }else{
                $this->error('个人资料修改失败！');
            }
        }
        if(!empty($edit['ubday'])){
            $edit['ubday'] = date('Y-m-d H:m:s', $edit['ubday']);
        }
        $this->assign([
            'postdb' => $edit
        ]);
        return view($this->temp.'user_edit.htm');
    }
    //  处理用户字段
    protected function addfiled($data){
        $list = array();
        foreach ($data as $k => $v){
            if(is_array($v)){
                $v = implode(',',$v);
            }
            $list[$k] = $v;
        }
        return $list;
    }
    //  写入session
    public function _session($data){
        unset($data['upassword']);
        session('userdb',$data);
    }

    // 添加收藏
    public function shoucang(){
        if(request()->isPost()){
            $cxmodel = new cxModel();
            $data = input('post.');
            $data = datatrim($data);
            $data['uid'] = $this->cxbsuser['uid'];
            if($cxmodel->shoucang($data)){
                $this->cxbsuser = session('userdb');
                $this->success("收藏成功！");
            }else{
                $this->error("收藏失败！");
            }
        }
        return false;
    }
    //  读取收藏信息
    public function ucollection(){
        $artmodel = new \app\common\model\Article();
        $ucoll = explode(',',$this->cxbsuser['ucollection']);
        $newucoll = null;
        if(!empty($ucoll)){
            foreach ($ucoll as $k => $v){
                $art = $artmodel->where('aid',$v)->find();
                if(!empty($art) && $art['status'] == '1'){
                    $newucoll[$k] = $artmodel->get_onedata($art);
                }else{
                    continue;
                }
            }
        }
        $this->assign([
            'list' => $newucoll,
        ]);
        return view($this->temp.'user_ucollection.htm');
    }
    //  处理自订义字段
    protected function addeditbase($data,$olddata=''){
        $userdata = unserialize($this->webdb['userdata']);
        if(!$userdata){
            return $data;
        }
        $fielddb = $userdata['field_db'];
        $upmodel = new Upload();
        $pirdir = date("Ym");
        foreach ($fielddb as $k => $v){
            if(!isset($data[$k]) || empty($data[$k]) || $data[$k] == null){
                continue;
            }
            if($v['formrequired'] == '1'){
                if(empty($data[$k]) || !isset($data[$k])){
                    $this->error("{$v['title']}不得为空");
                }
            }
            if(is_array($data[$k]) || isset($data[$k])){
                switch ($v['formtype']){
                    case 'textarea':
                        $data[$k] = str_replace('，',',',$data[$k]);
                        $data[$k] = preg_replace("/\r\n/","<br>",$data[$k]);
                        break;
                    case 'upfile':
                        if(!empty($olddata[$k])){
                            if($data[$k] == $olddata[$k]){
                                $data[$k] = $upmodel->editadd($data[$k]);
                                continue;
                            }else{
                                @unlink(ROOT_PATH.$this->webdb['updir'].$olddata[$k]);
                            }
                        }
                        $arr = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($data[$k],$pirdir);
                        $data[$k] = $upmodel->editadd($arr);
                        unset($arr);
                        break;
                    case 'upmv':
                        if(!empty($olddata[$k])){
                            if($data[$k] == $olddata[$k]){
                                $data[$k] = $upmodel->editadd($data[$k]);
                                continue;
                            }else{
                                @unlink(ROOT_PATH.$this->webdb['updir'].$olddata[$k]);
                            }
                        }
                        $arr = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($data[$k],$pirdir);
                        $data[$k] = $upmodel->editadd($arr);
                        unset($arr);
                        break;
                    case 'uptxt':
                        $oldfiles = $newfiles = '';
                        $arr = '';
                        if(!empty($olddata[$k])){
                            foreach ($olddata[$k] as $val){
                                $oldfiles[] = $val['value'];
                            }
                        }
                        foreach ($data[$k] as $key => $val){
                            if(!empty($oldfiles)){
                                if(in_array($val,$oldfiles)){
                                    $newfiles[] = $val;
                                    $val = $upmodel->editadd($val);
                                    $arr[] = $key.'@@@'.$val;
                                    continue;
                                }
                            }
                            $newfiles[] = $val;
                            $val = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($val,$pirdir);
                            $val = $upmodel->editadd($val);
                            $arr[] = $key.'@@@'.$val;
                        }
                        if(!empty($oldfiles)){
                            foreach ($oldfiles as $val){
                                if(!in_array($val,$newfiles)){
                                    @unlink(ROOT_PATH.$this->webdb['updir'].$val);
                                }
                            }
                        }
                        $oldfiles = $newfiles = null;
                        $data[$k] = implode('&@&@&',$arr);
                        unset($arr);
                        break;
                    case 'upimg':
                        $oldfiles = $newfiles = '';
                        $arr = '';
                        if(!empty($olddata[$k])){
                            foreach ($olddata[$k] as $val){
                                $oldfiles[] = $val['value'];
                            }
                        }
                        foreach ($data[$k] as $key => $val){
                            if(!empty($oldfiles)){
                                if(in_array($val,$oldfiles)){
                                    $newfiles[] = $val;
                                    $val = $upmodel->editadd($val);
                                    $arr[] = $key.'@@@'.$val;
                                    continue;
                                }
                            }
                            $newfiles[] = $val;
                            $val = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($val,$pirdir);
                            $val = $upmodel->editadd($val);
                            $arr[] = $key.'@@@'.$val;
                        }
                        if(!empty($oldfiles)){
                            foreach ($oldfiles as $val){
                                if(!in_array($val,$newfiles)){
                                    @unlink(ROOT_PATH.$this->webdb['updir'].$val);
                                }
                            }
                        }
                        $oldfiles = $newfiles = null;
                        $data[$k] = implode('&@&@&',$arr);
                        unset($arr);
                        break;
                    case 'uppaly':
                        $oldfiles = $newfiles = '';
                        $arr = '';
                        if(!empty($olddata[$k])){
                            foreach ($olddata[$k] as $val){
                                $oldfiles[] = $val['value'];
                            }
                        }
                        foreach ($data[$k] as $key => $val){
                            if(!empty($oldfiles)){
                                if(in_array($val,$oldfiles)){
                                    $newfiles[] = $val;
                                    $val = $upmodel->editadd($val);
                                    $arr[] = $key.'@@@'.$val;
                                    continue;
                                }
                            }
                            $newfiles[] = $val;
                            $val = '/'.$this->webdb['updir'].'/'.$upmodel->fileMove($val,$pirdir);
                            $val = $upmodel->editadd($val);
                            $arr[] = $key.'@@@'.$val;
                        }
                        if(!empty($oldfiles)){
                            foreach ($oldfiles as $val){
                                if(!in_array($val,$newfiles)){
                                    @unlink(ROOT_PATH.$this->webdb['updir'].$val);
                                }
                            }
                        }
                        $oldfiles = $newfiles = null;
                        $data[$k] = implode('&@&@&',$arr);
                        unset($arr);
                        break;
                    case 'ieedit':
                        // 处理内容中的图片
                        $pirdir = date("Ym");
                        $imgmove = array(
                            'oldimg' => $olddata[$k],
                            'newimg' => $data[$k],
                        );
                        $data[$k] = $upmodel->articleimg($imgmove,$pirdir);
                        break;
                    case 'time':
                        $data[$k] = strtotime($data[$k]);
                        break;
                    case 'checkbox':
                        $data[$k] = implode(',',$data[$k]);
                        break;
                    case 'chinacode':
                        $data[$k] = implode(',',$data[$k]);
                        break;
                }
            }
        }
        $content = null;
        foreach ($data as $k => $v){
            if($v == '0'){
                $content[$k] = $v;
                continue;
            }
            if(!isset($v) || empty($v)){
                continue;
            }
            $content[$k] = $v;
        }
        return $content;
    }
    //  对附加表进行读取解释
    protected function usmodelfields($data){
        $userdata = unserialize($this->webdb['userdata']);
        if(!$userdata){
            return $data;
        }
        $fielddb = $userdata['field_db'];
        $upmodel = new Upload();
        foreach ($fielddb as $k => $v){
            if(!isset($data[$k]) || empty($data[$k]) || $data[$k] == null){
                continue;
            }
            if($v['formtype'] == 'textarea'){
                $data[$k] = preg_replace('/<br>/',"\r",$data[$k]);
            }elseif($v['formtype'] == 'uptxt' || $v['formtype'] == 'upimg' || $v['formtype'] == 'upmv'){
                $data[$k] = explode('&@&@&',$data[$k]);
                $arr = array();
                foreach ($data[$k] as $key => $val){
                    $arr[] = explode('@@@',$val);
                }
                foreach ($arr as $value){
                    $arrs['title'] = $value[0];
                    $arrs['value'] = $upmodel->editadd($value[1],false);
                    $editdata[] = $arrs;
                }
                $data[$k] = $editdata;
                unset($arr);
                unset($arrs);
            }elseif($v['formtype'] == 'upfile'){
                $data[$k] = $upmodel->editadd($data[$k],false);
            }elseif($v['formtype'] == 'chinacode'){
                $data[$k] = explode(',',$data[$k]);
            }elseif($v['formtype'] == 'time'){
                $data[$k] = date('Y-m-d H:i:s',$data[$k]);
            }
        }
        return $data->toArray();
    }
}